package order.mapper;

import common.model.order.Orders;
import common.model.order.OrderItem;
import org.apache.ibatis.annotations.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper extends BaseMapper<Orders> {
    //增加订单
    @Insert("INSERT INTO `orders` (order_no, user_id, total_amount, item_amount, freight_amount, discount_amount, " +
            "pay_amount, payment_type, payment_status, recipient_name, recipient_phone, province, city, district, " +
            "address_detail, postal_code, status, create_time, update_time) " +
            "VALUES(#{orderNo}, #{userId}, #{totalAmount}, #{itemAmount}, #{freightAmount}, #{discountAmount}, " +
            "#{payAmount}, #{paymentType}, #{paymentStatus}, #{recipientName}, #{recipientPhone}, #{province}, " +
            "#{city}, #{district}, #{addressDetail}, #{postalCode}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertOrder(Orders order);

    //根据id查找订单
    @Select("SELECT * FROM `orders` WHERE id = #{orderId}")
    @ResultMap("OrderResultMap")
    Orders findById(Long orderId);

    //插入订单对应的商品项
    @Insert("INSERT INTO order_item(order_no, sku_id, spu_id, quantity, price, total_price, item_name, item_image, promotion_id, promotion_price) " +
            "VALUES(#{orderNo}, #{skuId}, #{spuId}, #{quantity}, #{price}, #{totalPrice}, #{itemName}, #{itemImage}, #{promotionId}, #{promotionPrice})")
    void insertOrderItem(OrderItem orderItem);

    //获得订单列表
    @Select("SELECT * FROM `orders` WHERE user_id = #{uid} ORDER BY create_time DESC")
    @ResultMap("OrderResultMap")
    List<Orders> findOrderByUid(Long uid);

    //根据订单id查询订单
    @Select("SELECT * FROM `orders` WHERE id = #{oid}")
    @ResultMap("OrderResultMap")
    Orders findOrderById(Long oid);

    //根据订单id删除商品项
    @Delete("DELETE FROM order_item WHERE order_no = (SELECT order_no FROM orders WHERE id = #{oid})")
    void deleteOrderItemsByOrderId(Long oid);

    //根据订单状态查询订单
    @Select("SELECT * FROM `orders` WHERE status = #{status} AND user_id = #{uid} ORDER BY create_time DESC")
    @ResultMap("OrderResultMap")
    List<Orders> findOrderByStatus(Integer status, Long uid);

    //更新订单状态
    @Update("UPDATE `orders` SET status = #{status}, payment_time = #{now}, update_time = #{now} WHERE id = #{orderId}")
    void updateOrderStatus(Long orderId, Integer status, LocalDateTime now);

    //根据订单id查询订单项
    @Select("SELECT * FROM `order_item` WHERE order_no = (SELECT order_no FROM orders WHERE id = #{oid})")
    @ResultMap("OrderItemResultMap")
    List<OrderItem> findOrderItemsByOrderId(Long oid);

    // 获取用户订单数量
    @Select("SELECT COUNT(*) FROM `orders` WHERE user_id = #{uid}")
    int findOrderCountByUid(Long uid);

    //根据订单编号查询订单项
    @Select("SELECT * FROM `order_item` WHERE order_no = #{orderNo}")
    @ResultMap("OrderItemResultMap")
    List<OrderItem> findOrderItemsByOrderNo(String orderNo);


    //后台
    //获取最近的10条订单
    @Select("SELECT * FROM `orders` ORDER BY create_time DESC LIMIT 10")
    List<Orders> findRecentOrders();
}
